#include "widget.h"
#include "ui_widget.h"
#include"directory.h"
#include"gameplay.h"
#include"web.h"
#include"downfall.h"
#include"downquick.h"

#include<random>

#define TIMEOUT 200
int Widget::npcID = 0;//供全局使用
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    myTimerId=this->startTimer(TIMEOUT);
    picID=2;
    npcID=1;
    QPixmap pix("D:\\Code\\Qt\\DrChen\\rsc\\npc\\1idle_0");
    QPixmap scaledPix = pix.scaled(40, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->npcLabel->setPixmap(scaledPix);
}

Widget::~Widget()
{
    delete ui;
    this->killTimer(myTimerId);
}

void Widget::on_dirButton_clicked()
{
    this->hide();
    Directory *d=new Directory;
    d->show();
}


void Widget::on_startButton_clicked()
{
    this->hide();
    GamePlay *g=new GamePlay;
    g->show();
}




void Widget::on_randomButton_clicked()
{
    npcID=getRandomNumberBetween(npcID);
}

int Widget::getRandomNumberBetween(int npcID) {
    // 创建一个随机数引擎

    std::random_device rd;
    std::mt19937 gen(rd());

    // 创建一个在1到5之间（包含1和5）的均匀分布
    std::uniform_int_distribution<> dis(1, 5);

    // 生成并返回随机数
    int randomNumber;
    do {
        // 生成随机数
        randomNumber = dis(gen);
    } while (randomNumber == npcID); // 如果随机数等于npcID，则重新生成

    return randomNumber;
}

void Widget::on_webButton_clicked()
{
    //this->hide();
    Web *w=new Web;
    w->show();
}


void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId()!=myTimerId)
        return;


    QString path("D:\\Code\\Qt\\DrChen\\rsc\\npc\\idle\\");
    path+=QString::number(npcID);
    path+="idle_";
    path+=QString::number(picID);
    path+=".png";

    QPixmap pix(path);
    QPixmap scaledPix = pix.scaled(30, 45, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->npcLabel->setPixmap(scaledPix);

    picID++;
    if(4==picID)
    {
        picID=1;
    }
}




void Widget::on_downfallButton_clicked()
{
    this->hide();
    DownFall *d=new DownFall;
    d->show();
}


void Widget::on_downQuickButton_clicked()
{
    this->hide();
    DownQuick *Q=new DownQuick;
    Q->show();
}
